home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 16045 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.8 KB  |  113 lines

  1. Path: grimsel.zurich.ibm.com!usenet
  2. From: wgk@zurich.ibm.com (Keith Whittingham)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Arrays or Pointers?
  5. Date: 9 Apr 1996 09:33:45 GMT
  6. Organization: IBM Research, ZRH
  7. Message-ID: <4kdatp$kcp@grimsel.zurich.ibm.com>
  8. References: <4kbsk3$f07@zeus.tcp.co.uk>
  9. Reply-To: wgk@zurich.ibm.com
  10. NNTP-Posting-Host: pine.zurich.ibm.com
  11. X-Newsreader: IBM NewsReader/2 v1.00
  12.  
  13. In <4kbsk3$f07@zeus.tcp.co.uk>, daveg@tcp.co.uk (David) writes:
  14. >I'm currently on a C++ course and we seem to be using pointers a fair
  15. >bit. On questioning this, was told that pointers where quicker than
  16. >using arrays. In which situations should I be using arrays over
  17. >pointers and vice versa?
  18. >
  19.  
  20. It's a real shame there's not more C/C++ programmers who understand
  21. the code produced by the compiler in order to implement the code. 
  22. You need only a simplistic understanding of assembler and a bit 
  23. of curiosity - understanding it also makes it very easy to
  24. clear up bugs like:
  25.  
  26.    if(x = y);
  27.      puts(x)
  28.  
  29. etc.
  30.  
  31. Anyway, I digress. There is no reason why pointers should be faster
  32. or slower than arrays. Multi dimensioned arrays tend to be slower
  33. because of the way a program might access them only. Take for
  34. example a 25 x 80 character screen. Using array:
  35.  
  36.     char Screen[25][80];
  37.     int r = 2;
  38.     int c = 0;
  39.  
  40.     Screen[r][c++] = 'A';
  41.     Screen[r][c++] = 'B';
  42.     Screen[r][c++] = 'C';
  43.  
  44.     p = &Screen[2][0];
  45.     *p++ = 'A';
  46.     *p++ = 'B';
  47.     *p = 'C';
  48.  
  49. and using pointers...
  50.  
  51.     char Screen[25*80];
  52.     char *p;
  53.  
  54.     p = &Screen[2*80];
  55.     *p++ = 'A';
  56.     *p++ = 'B';
  57.     *p = 'C';
  58.  
  59. I'm not saying either of the above is how it should be done but I
  60. am suggesting that if you looked of the shoulder of Mr AverageProgrammer
  61. that's what you might find. Now expand with the assembler...
  62.  
  63. Arrays...
  64.  
  65.     int r = 2;
  66.     int c = 0;
  67.         mov    cx,0002
  68.         xor    dx,dx
  69.  
  70.     Screen[r][c++] = 'A';
  71.         mov    bx,cx
  72.         imul   bx,0050
  73.         add    bx,dx
  74.         mov    byte ptr [bx+02A0],41
  75.         inc    dx
  76.     Screen[r][c++] = 'B';
  77.         mov    bx,cx
  78.         imul   bx,0050
  79.         add    bx,dx
  80.         mov    byte ptr [bx+02A0],42
  81.         inc    dx
  82.     Screen[r][c++] = 'C';
  83.         mov    bx,cx
  84.         imul   bx,0050
  85.         add    bx,dx
  86.         mov    byte ptr [bx+02A0],43
  87.         inc    dx
  88.  
  89.  
  90. Pointers...
  91.  
  92.     p = &Screen[2*80];
  93.         mov    si,0340
  94.     *p++ = 'A';
  95.         mov    byte ptr [si],41
  96.         inc    si
  97.     *p++ = 'B';
  98.         mov    byte ptr [si],42
  99.         inc    si
  100.     *p = 'C';
  101.         mov    byte ptr [si],43
  102.  
  103. The mutiplication instruction (imul in this case) tends to be quite 
  104. thirsty and one imul could burn as many clock cycles as all 6 lines
  105. of assembler in the pointers example.
  106.  
  107. After seeing the above example you'll probably make the correct 
  108. choice yourself...
  109.  
  110. Keith
  111.  
  112.  
  113.